﻿using BaseFrame.Entity;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BaseFrame.Tool
{
    public class EPPlusExcel
    {

        ///// <summary>
        ///// 导出示例
        ///// </summary>
        //public void ExportDataInfo()
        //{

        //    SaveFileDialog saveFileDialog = new SaveFileDialog()
        //    {
        //        Title = "请选择存储的路径+文件名称",
        //        FileName = "导出评论数据",
        //        Filter = "csv文件(*.csv)|*.csv"
        //    };
        //    if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        //    {
        //        if (string.IsNullOrEmpty(saveFileDialog.FileName))
        //        {
        //            MessageBox.Show(this, "文件夹路径不能为空", "提示");
        //            return;
        //        }
        //    }
        //    else
        //    {
        //        var data = new List<ShopListInfoEntity>();
        //        string fileName = saveFileDialog.FileName.Replace(".csv", ".xlsx");
        //        FileInfo newFile = new FileInfo(fileName);
        //        if (newFile.Exists)
        //        {
        //            newFile.Delete();
        //            newFile = new FileInfo(fileName);
        //        }
        //        var entityDescriptBinds = Global.sqlSugarClient.GetPropertyListDescrity<ShopListInfoEntity>();
        //        string errorMsg = "";
        //        ExportData(data, entityDescriptBinds, newFile, ref errorMsg);
        //    }
        //}

        /// <summary>
        /// 导出excel
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="data">导出数据List</param>
        /// <param name="entityDescriptBinds">数据绑定的表头+导出的数据列名称</param>
        /// <param name="fileInfoEntity">导出文件信息</param>
        /// <param name="errorMsg">异常错误消息</param>
        /// <param name="sheetName">工作单元格名称</param>
        /// <returns></returns>
        public static bool ExportData<T>(List<T> data, List<EntityDescriptBind> entityDescriptBinds, FileInfo fileInfoEntity, ref string errorMsg, string sheetName = "数据信息")
        {
            try
            {
                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                using (ExcelPackage package = new ExcelPackage(fileInfoEntity))
                {
                    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(sheetName);
                    for (int i = 0; i < entityDescriptBinds.Count; i++)
                    {
                        worksheet.Cells[1, i + 1].Value = entityDescriptBinds[i].FieldDescription;
                    }
                    for (int i = 0; i < data.Count; i++)
                    {
                        worksheet.Cells[2 + i, 1].Value = i + 1;
                        var tempData = data[i];
                        for (int j = 0; j < entityDescriptBinds.Count; j++)
                        {
                            worksheet.Cells[2 + i, 2].Value = GetDataValue(entityDescriptBinds[i], tempData);
                        }
                    }
                    package.Save();
                }
                return true;
            }
            catch (Exception ex)
            {
                errorMsg = ex.ToString();
                return false;
            }
        }

        private static object GetDataValue<T>(EntityDescriptBind entityDescriptBind, T tempData)
        {
            Type t = tempData.GetType();
            var pros = t.GetProperties();
            foreach (var item in pros)
            {
                if (item.Name.Equals(entityDescriptBind.FieldName))
                {
                    object result = item.GetGetMethod().Invoke(tempData, null);
                    return result;
                }
            }
            return "";
        }
    }
}
